package cn.lhf.myblog.service.impl;

import cn.lhf.myblog.db.entity.User;
import cn.lhf.myblog.db.repository.UserRepository;
import cn.lhf.myblog.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserRepository userRepository;

    @Override
    public User loginByName(String username, String password) {
        return userRepository.findUserByUsernameAndPassword(username, password);
    }

    @Override
    public User register(User user) {
        if (userRepository.existsUserByUsername(user.getUsername())) {
            return null;
        } else if (userRepository.existsUserByEmail(user.getEmail())) {
            return null;
        }
        return userRepository.save(user);
    }

    @Override
    public User getUserById(int id) {
        User user = userRepository.findUserById(id);
        if (user == null) {
            return null;
        }
        user.setPassword(null);
        return user;
    }
}
